/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-26 11:14:33
 */
import { AmbientLight, Color, DirectionalLight, Material, Mesh, MeshLambertMaterial, Vector3 } from 'three';
import ThreeBase from 'three-base';
import threeUtils from 'three-base/threeUtils';
import MeshBase from 'three-base/MeshBase';
import PointLight from 'three-base/PointLight';
import utils from '@utils';
import SpriteText from 'three-base/SpriteText';
import keyContorls from 'three-base/keyContorls';
import { round } from 'lodash';
import Sky from 'three-base/Sky';
import bloom_pass from './component3d/bloom_pass/bloom_pass';

/**
 *
 *
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
  constructor() {
    super();
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight('#fff', 1.5);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //点光源
  pointLight = (() => {
    const pointLight = new PointLight('#fff', 50);
    pointLight.position.set(0, 3, -3);
    pointLight.shadow.bias = -0.001;
    this.scene.add(pointLight);
    return pointLight;
  })();

  // 方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight('#fff', 1.5);
    directionalLight.position.set(-10, 10, 5);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.bias = -0.001;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  init() {
    super.init();
    this.gridHelper.visible = false;

    this.scene.background = new Color('#212933');

    // 开发模式时还原记录的位置
    if (threeUtils.isDev && localStorage.lookAt) {
      this.resetLookAt({
        ...JSON.parse(localStorage.lookAt),
      });
    }

    // 开发模式时记录当前的位置
    if (threeUtils.isDev) {
      this.controls.addEventListener('change', () => {
        localStorage.lookAt = JSON.stringify(this.getLookAt());
      });
    }

    if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
      this.resetLookAt({
        center: new Vector3(0, 0, 0),
        position: new Vector3(-7.049, 27.469, 59.276),
      });
    }
  }

  //天空盒
  sky = (() => {
    const sky = new Sky();
    sky.material.map = null;
    sky.material.color.set('#212933');
    this.scene.add(sky);
    return sky;
  })();

  //T102
  lableT102 = (() => {
    const lableT102 = new SpriteText('T102');
    lableT102.strokeColor = '#000000';
    lableT102.strokeWidth = 1;
    lableT102.scale.multiplyScalar(0.05);
    lableT102.position.set(-9.9, 2.6, 8.6);
    this.add(lableT102);
    return lableT102;
  })();

  houseMesh = (() => {
    const houseMesh = new MeshBase('./房子调色.glb');
    houseMesh.addEventListener('loaded', () => {
      houseMesh.traverseMesh((mesh) => {
        mesh.frustumCulled = false;
        (mesh.material as MeshLambertMaterial).depthWrite = true;
      });
      //   threeUtils.applyAllTranstion(houseMesh);
      houseMesh.traverse((mesh) => {
        //three事件
        mesh.userData = {
          cursor: 'pointer',
          enableEvent: false,
        };

        mesh.addEventListener('click', (e) => {
          console.log(mesh.name);
        });
        mesh.addEventListener('dbclick', (e) => {
          utils.editorMaterial((mesh as Mesh).material as any, mesh.name);
        });

        // 房子的主体颜色
        if (['立方体031_1'].includes(mesh.name)) {
          ((mesh as Mesh).material as MeshLambertMaterial).color.set('#afcbea');
        }

        if (['T305'].includes(mesh.name)) {
          mesh.userData = {
            glow_enable: false, //启用发光
            glow_color: '#ccf1ff', //发光颜色
            glow_width: 10, //发光厚度(像素)
            glow_opacity: 0, //发光透明度(像素)
          };
        }

        //发光模型
        if (['outline047'].includes(mesh.name)) {
          ((mesh as Mesh).material as MeshLambertMaterial).color.set('#afcbea');
          ((mesh as Mesh).material as MeshLambertMaterial).emissive.set('#afcbea');
          mesh.userData = {
            glow_enable: false, //启用发光
            glow_color: '#afcbea', //发光颜色
            glow_width: 20, //发光厚度(像素)
            glow_opacity: 0.5, //发光透明度(像素)
          };
        }

        if (['outline'].includes(mesh.name)) {
          mesh.userData = {
            glow_enable: false, //启用发光
            glow_color: '#557BB1', //发光颜色
            glow_width: 20, //发光厚度(像素)
            glow_opacity: 0.5, //发光透明度(像素)
          };
        }
      });
    });
    this.add(houseMesh);
    return houseMesh;
  })();

  render() {
    super.render();
    const T305 = this.scene.getObjectByName('T305');
    if (T305) {
      bloom_pass(this.renderer, this.getCamera(), T305, this.baseRenderTarget, this.baseDepthTexture);
    }
    const outline047 = this.scene.getObjectByName('outline047');
    if (outline047) {
      // bloom_pass(this.renderer, this.getCamera(), outline047, this.baseRenderTarget, this.baseDepthTexture);
    }
  }
}

const homeRenderer = (window.homeRenderer = new HomeRender());

export default homeRenderer;
